package com.example.script.portal.service.impl;

import com.example.script.mapper.ChooseScriptMapper;
import com.example.script.mapper.ScriptNameMapper;
import com.example.script.model.ChooseScript;
import com.example.script.model.ScriptName;
import com.example.script.model.ScriptNameExample;
import com.example.script.portal.model.dto.ScriptChooseParam;
import com.example.script.portal.model.vo.ScriptListVo;
import com.example.script.portal.service.ProductService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ScriptNameMapper scriptMapper;

    @Autowired
    private ChooseScriptMapper chooseScriptMapper;

    @Override
    public List<ScriptListVo> list(int pageNum, int pageSize) {
        List<ScriptListVo> scriptListVos = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        ScriptNameExample scriptExample = new ScriptNameExample();
        List<ScriptName> scriptList = scriptMapper.selectByExample(scriptExample);
        for (ScriptName script : scriptList) {
            ScriptListVo scriptListVo = new ScriptListVo();
            Long scriptId = script.getId();

            scriptListVo.setsId(scriptId);
            scriptListVo.setScriptName(script.getScriptname());
            scriptListVo.setScriptType(script.getScripttype());
            scriptListVo.setScriptNOP(script.getScriptnop());
            scriptListVo.setScriptDuration(script.getScriptDuration());
            scriptListVo.setScriptShortIntro(script.getScriptShortIntroduction());
            scriptListVo.setScriptDetailIntro(script.getScriptDetailedIntroduction());

            scriptListVos.add(scriptListVo);
        }
        return scriptListVos;
    }

    public List<ScriptListVo> findByName(String scriptname) {
        List<ScriptListVo> scriptListVos = new ArrayList<>();
        ScriptNameExample scriptExample = new ScriptNameExample();
        ScriptNameExample.Criteria criteria = scriptExample.createCriteria();
        criteria.andScriptnameEqualTo(scriptname);

        List<ScriptName> scriptList = scriptMapper.selectByExample(scriptExample);
        for (ScriptName script : scriptList) {
            ScriptListVo scriptListVo = new ScriptListVo();
            String scriptName = script.getScriptname();

            scriptListVo.setScriptName(scriptName);
            scriptListVo.setsId(script.getId());
            scriptListVo.setScriptName(script.getScriptname());
            scriptListVo.setScriptType(script.getScripttype());
            scriptListVo.setScriptNOP(script.getScriptnop());
            scriptListVo.setScriptDuration(script.getScriptDuration());
            scriptListVo.setScriptShortIntro(script.getScriptShortIntroduction());
            scriptListVo.setScriptDetailIntro(script.getScriptDetailedIntroduction());

            scriptListVos.add(scriptListVo);
        }
        return scriptListVos;
    }

    public int chooseScript(ScriptChooseParam param) {
        int result = 0;

        ChooseScript chooseScript = new ChooseScript();
        chooseScript.setScriptId(param.getScriptId());
        chooseScript.setUserId(param.getUserId());
        chooseScript.setStateCode(param.getStateCode());
        chooseScript.setInvitationCode(param.getInvitationCode());

        result = chooseScriptMapper.insertSelective(chooseScript);
        if (result <= 0) {
            return 0;
        }
        return result;
    }
}


